<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="单据类型" prop="docId">
            <select-single-wms-doc v-model:doc-id="formData.docId" v-model:doc-no="formData.code"
                                   :is-gen-no="formType === 'create'" doc-type="wms_other_in" :disabled="formType === 'update'"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="单据编号" prop="code">
            <el-input v-model="formData.code" placeholder="请输入单据编号" disabled />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="单据日期" prop="docDate">
            <el-date-picker
                v-model="formData.docDate"
                type="date"
                value-format="x"
                placeholder="选择单据日期"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="审核状态" prop="approveStatus">
            <el-radio-group v-model="formData.approveStatus" disabled>
              <el-radio
                  v-for="dict in getBoolDictOptions(DICT_TYPE.APPROVE_STATUS)"
                  :key="dict.value"
                  :label="dict.value"
              >
                {{ dict.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="制单人" prop="operateUser">
            <select-user v-model:user-id="formData.operateUser" v-model:dept-id="formData.operateDept"/>
          </el-form-item>
        </el-col>
      </el-row>
<!--      <el-row>
        <el-col :span="12">
          <el-form-item label="业务合计数量" prop="businessSumQty">
            <el-input v-model="formData.businessSumQty" placeholder="请输入业务合计数量" disabled/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="成本合计金额" prop="costSumAmt">
            <el-input v-model="formData.costSumAmt" placeholder="请输入成本合计金额" disabled/>
          </el-form-item>
        </el-col>
      </el-row>-->
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="formData.remark" type="textarea" placeholder="请输入备注" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="附件" prop="annex">
            <UploadFile v-model="formData.annex" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <!-- 子表的表单 -->
    <el-tabs v-model="subTabsName">
      <el-tab-pane label="入库明细" name="otherInD">
        <div style="margin-top: 10px">
          <OtherInDForm ref="otherInDFormRef" :head-id="formData.id" />
        </div>
      </el-tab-pane>
    </el-tabs>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading" v-if="!formData.approveStatus">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getBoolDictOptions, DICT_TYPE } from '@/utils/dict'
import * as OtherInApi from '@/api/wms/otherin'
import OtherInDForm from './components/OtherInDForm.vue'
import SelectSingleWmsDoc from "@/views/wms/doc/components/SelectSingleWmsDoc.vue";
import SelectUser from "@/views/system/user/components/SelectUser.vue";

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  docId: undefined,
  docDate: undefined,
  code: undefined,
  transactionDate: undefined,
  operateUser: undefined,
  operateDept: undefined,
  operateDate: undefined,
  approveUser: undefined,
  approveDate: undefined,
  businessSumQty: 0,
  costSumAmt: 0,
  approveStatus: false,
  annex: undefined,
  remark: undefined,
})
const formRules = reactive({
  docId: [{ required: true, message: '单据类型不能为空', trigger: 'blur' }],
  docDate: [{ required: true, message: '单据日期不能为空', trigger: 'blur' }],
  code: [{ required: true, message: '编号不能为空', trigger: 'blur' }],
  operateUser: [{ required: true, message: '制单人不能为空', trigger: 'blur' }],
  approveStatus: [{ required: true, message: '审核状态不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 子表的表单 */
const subTabsName = ref('otherInD')
const otherInDFormRef = ref()

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await OtherInApi.getOtherIn(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 选中用户*/
const changeUser = (value)=> {
  console.log(value);
}

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 校验子表单
  try {
    await otherInDFormRef.value.validate()
  } catch (e) {
    subTabsName.value = 'otherInD'
    return
  }
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as OtherInApi.OtherInVO
    // 拼接子表的数据
    data.otherInDs = otherInDFormRef.value.getData()
    if (formType.value === 'create') {
      await OtherInApi.createOtherIn(data)
      message.success(t('common.createSuccess'))
    } else {
      await OtherInApi.updateOtherIn(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    docId: undefined,
    docDate: Date.now(),
    code: undefined,
    transactionDate: undefined,
    operateUser: undefined,
    operateDept: undefined,
    operateDate: undefined,
    approveUser: undefined,
    approveDate: undefined,
    businessSumQty: 0,
    costSumAmt: 0,
    approveStatus: false,
    annex: undefined,
    remark: undefined,
  }
  formRef.value?.resetFields()
}
</script>
